//
//  TopicListViewController.m
//  Leisure
//
//  Created by xalo on 16/7/2.
//  Copyright © 2016年 朱旭东. All rights reserved.
//

#import "TopicListViewController.h"

@interface TopicListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,assign)NSInteger Start;//列表数据起始位置
@property(nonatomic,assign)NSInteger limit;//列表的条数

@end

@implementation TopicListViewController

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray =[NSMutableArray array] ;
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self loadTableViewDataSourceWithStart:0 limit:10];
    [self.tableView registerNib:[UINib nibWithNibName:@"TopicListCell" bundle:nil] forCellReuseIdentifier:@"topicList_cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TopicCell" bundle:nil] forCellReuseIdentifier:@"topic_cell"];
    
#pragma mark ------------------ 下拉刷新上拉加载----------------------

    //下拉刷新
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        //刷新之前清空数组
        [self.dataArray removeAllObjects];
        self.Start= 0;
        [self  loadTableViewDataSourceWithStart:0 limit:10];
    }];
    
    //添加上拉加载,执行完之后要结束
    self.tableView.mj_footer = [MJRefreshBackGifFooter  footerWithRefreshingBlock:^{
        self.Start += 10;
        [self  loadTableViewDataSourceWithStart:self.Start limit:10];
    }];
    
}
#pragma mark -----------加载数据---------------
- (void)loadTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
    [RequestManager fetchDataWithURL:kTopicListUrl parameters:@{@"contentid":self.contentId} method:@"POST" complet:^(NSData *data) {

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
         NSLog(@"%@",dic);
        for (NSDictionary *subdic in dic[@"data"][@"commentlist"]) {
            TopicListModel *model = [[TopicListModel alloc]init];
            [model  setValuesForKeysWithDictionary:subdic];
            [model setValuesForKeysWithDictionary:subdic[@"userinfo"]];
            [self.dataArray addObject:model];
        }
        
    } updataUI:^{
        [self.tableView.mj_footer  endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
    }];
}
#pragma mark -----------UItableViewDelegate---------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   

    if (indexPath.row == 0) {
        TopicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topic_cell" forIndexPath:indexPath];
        [cell setCellWithModel:self.model_one_cell];
        cell.backgroundColor = [UIColor  groupTableViewBackgroundColor];
            return cell;
    }else{
        TopicListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topicList_cell" forIndexPath:indexPath];
        TopicListModel *model = self.dataArray[indexPath.row];
        [cell  setCellWithmModle:model];
    
            return cell;
    }

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 200;
    }else{
        return 150;
    }
}
@end
